package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/8/4 10:00
 */
@RequestMapping("/sysDictData")
@RestController
@CrossOrigin
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;


    //添加字典数据
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> sysDictDataVO) {

        sysDictDataService.add(sysDictDataVO);
        return new Result("success", "添加字典数据成功");
    }

    //批量删除字典数据
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Map<String, Object>> sysDictDataVoList) {
        sysDictDataService.deleteBatch(sysDictDataVoList);
        return new Result("success", "删除成功");
    }

    //编辑字典数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String, Object> sysDictDataVO) {
        sysDictDataService.edit(sysDictDataVO);
        return new Result("success", "编辑成功");
    }

    //获取字典数据列表
    @RequestMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> sysDictDataVO) {
        Page<SysDictData> dictDataList = (Page<SysDictData>) sysDictDataService.getList(sysDictDataVO);
        PageResult<SysDictData> result = new PageResult<>();
        result.setRecords(dictDataList);
        result.setTotal(dictDataList.getTotal());
        result.setCurrent((long) dictDataList.getPageNum());
        result.setOptimizeCountSql(true);
        result.setSearchCount(true);
        return new Result("success", dictDataList);

    }

    //根据字典类型获取字典数据
    @RequestMapping("/getListByDictType")
    public Result getListByDictType(@RequestParam String dictType) {
        List<SysDictData> sysDictData = sysDictDataService.getListByDictType(dictType);
        return new Result("success", sysDictData);
    }

    //根据字典类型数组获取字典数据
    @RequestMapping("/getListByDictTypeList")
    public Result getListByDictTypeList(@RequestBody String[] dictTypeList) {
        List<SysDictData> listByDictTypeList = sysDictDataService.getListByDictTypeList(dictTypeList);
//        PageResult<SysDictData> result = new PageResult<>();
//        result.setRecords(listByDictTypeList);
//        result.setTotal(listByDictTypeList.getTotal());
//        result.setCurrent((long) listByDictTypeList.getPageNum());
//        result.setOptimizeCountSql(true);
//        result.setSearchCount(true);
        return new Result("success", listByDictTypeList);
    }


}
